﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MIT.SysAdmin
{
    public partial class SysAdminDivisionCreator : System.Web.UI.Page
    {
        String Req1;
        String Req2;
        String Req3;

        String NewsDate1;
        String NewsDate2;
        String NewsDate3;
        String NewsTitle1;
        String NewsTitle2;
        String NewsTitle3;
        String NewsDetails1;
        String NewsDetails2;
        String NewsDetails3;

        String BudgetValue;
        int BudgetInt;
        protected void Page_Load(object sender, EventArgs e)
        {
            LabelTotalBudget.Text = "";

            String Name = (String)Session["LoggingInfo"];
            String Position = (String)Session["LoggingPosition"];
            String username = (String)Session["LoggingInfoUsername"];
            LabelDivAdminName.Text = Name;
            LabelDivAdminPosition.Text = "(" + Position + ")";

            ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;

            Session["CalLoggingInfo"] = Name;
            Session["CalLoggingPosition"] = Position;
            Session["CalLoggingInfoUsername"] = username;

            ///////////////////Notification Number//////////////////////////////////////////////////
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spMsgCounter", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    Req1 = Convert.ToString(dr["REQUEST1"]);
                    Req2 = Convert.ToString(dr["REQUEST2"]);
                    Req3 = Convert.ToString(dr["REQUEST3"]);
                }

            }
            if (Req1 == "")
            {
                LabelMsgNo.Text = "";

            }
            else if (Req2 == "")
            {
                LabelMsgNo.Text = "1";
            }
            else if (Req3 == "")
            {
                LabelMsgNo.Text = "2";
            }
            else
            {
                LabelMsgNo.Text = "3";
            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews1", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News1 = new SqlParameter("@News1", 1);
                GetNews.Parameters.Add(News1);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate1 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle1 = Convert.ToString(dr["Title"]);
                    NewsDetails1 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews2", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News2 = new SqlParameter("@News2", 2);
                GetNews.Parameters.Add(News2);


                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate2 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle2 = Convert.ToString(dr["Title"]);
                    NewsDetails2 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews3", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News3 = new SqlParameter("@News3", 3);
                GetNews.Parameters.Add(News3);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate3 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle3 = Convert.ToString(dr["Title"]);
                    NewsDetails3 = Convert.ToString(dr["Details"]);
                }

            }


            LabelDetails1.Text = NewsDetails1;
            LabelDetails2.Text = NewsDetails2;
            LabelDetails3.Text = NewsDetails3;

            LabelNews1.Text = NewsTitle1;
            LabelNews2.Text = NewsTitle2;
            LabelNews3.Text = NewsTitle3;

            LabelDate1.Text = NewsDate1;
            LabelDate2.Text = NewsDate2;
            LabelDate3.Text = NewsDate3;


            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spGetTotalBudget", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@yearvalue", Convert.ToInt32(DropDownListYear.SelectedValue));
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    BudgetValue = Convert.ToString(dr["BUDGETVALUE"]);
                    LabelTotalBudget.Text = Convert.ToString(BudgetValue);
                }

            }

        }

        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Login/SignIn.aspx");
        }

        protected void ButtonDivAdd_Click(object sender, EventArgs e)
        {
            BudgetInt = Convert.ToInt32(BudgetValue) - Convert.ToInt32(TextBoxDivBudget.Text);

            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spUpdateBudget", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@yearvalue", BudgetInt);
                cmd.Parameters.AddWithValue("@comyear", Convert.ToInt32(DropDownListYear.SelectedValue));

                con.Open();
                cmd.ExecuteNonQuery();
            }
       
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spAddDivision", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Division", DropDownListDept.SelectedValue);
                cmd.Parameters.AddWithValue("@DivisionEmpNo", TextBoxDivEmp.Text);
                cmd.Parameters.AddWithValue("@DivisionBudget", TextBoxDivBudget.Text);




                con.Open();
                cmd.ExecuteNonQuery();
                Response.Redirect("SysAdminDivisionCreator.aspx");
            }
        }
    }
}